<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - code analysis - src/caffe/layers/data_layer.cpp</title>
  <link rel="stylesheet" type="text/css" href="../../../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../../../index.html">top level</a> - <a href="index.html">src/caffe/layers</a> - data_layer.cpp<span style="font-size: 80%;"> (source / <a href="data_layer.cpp.func-sort-c.html">functions</a>)</span></td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">code analysis</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">73</td>
            <td class="headerCovTableEntry">74</td>
            <td class="headerCovTableEntryHi">98.6 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2020-09-11 22:50:33</td>
            <td></td>
            <td class="headerItem">Functions:</td>
            <td class="headerCovTableEntry">10</td>
            <td class="headerCovTableEntry">18</td>
            <td class="headerCovTableEntryLo">55.6 %</td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<a name="1"><span class="lineNum">       1 </span>            : #ifdef USE_OPENCV</a>
<span class="lineNum">       2 </span>            : #include &lt;opencv2/core/core.hpp&gt;
<span class="lineNum">       3 </span>            : #endif  // USE_OPENCV
<span class="lineNum">       4 </span>            : #include &lt;stdint.h&gt;
<span class="lineNum">       5 </span>            : 
<span class="lineNum">       6 </span>            : #include &lt;vector&gt;
<span class="lineNum">       7 </span>            : 
<span class="lineNum">       8 </span>            : #include &quot;caffe/data_transformer.hpp&quot;
<span class="lineNum">       9 </span>            : #include &quot;caffe/layers/data_layer.hpp&quot;
<span class="lineNum">      10 </span>            : #include &quot;caffe/util/benchmark.hpp&quot;
<span class="lineNum">      11 </span>            : 
<span class="lineNum">      12 </span>            : namespace caffe {
<a name="13"><span class="lineNum">      13 </span>            : </a>
<span class="lineNum">      14 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      15 </span><span class="lineCov">          2 : DataLayer&lt;Dtype&gt;::DataLayer(const LayerParameter&amp; param)</span>
<span class="lineNum">      16 </span>            :   : BasePrefetchingDataLayer&lt;Dtype&gt;(param),
<span class="lineNum">      17 </span><span class="lineCov">          4 :     offset_() {</span>
<span class="lineNum">      18 </span><span class="lineCov">          2 :   db_.reset(db::GetDB(param.data_param().backend()));</span>
<span class="lineNum">      19 </span><span class="lineCov">          4 :   db_-&gt;Open(param.data_param().source(), db::READ);</span>
<span class="lineNum">      20 </span><span class="lineCov">          2 :   cursor_.reset(db_-&gt;NewCursor());</span>
<span class="lineNum">      21 </span><span class="lineCov">          2 : }</span>
<a name="22"><span class="lineNum">      22 </span>            : </a>
<span class="lineNum">      23 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      24 </span><span class="lineCov">          4 : DataLayer&lt;Dtype&gt;::~DataLayer() {</span>
<span class="lineNum">      25 </span><span class="lineCov">          2 :   this-&gt;StopInternalThread();</span>
<span class="lineNum">      26 </span><span class="lineCov">          6 : }</span>
<a name="27"><span class="lineNum">      27 </span>            : </a>
<span class="lineNum">      28 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      29 </span><span class="lineCov">          2 : void DataLayer&lt;Dtype&gt;::DataLayerSetUp(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,</span>
<span class="lineNum">      30 </span>            :       const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">      31 </span><span class="lineCov">          2 :   const int batch_size = this-&gt;layer_param_.data_param().batch_size();</span>
<span class="lineNum">      32 </span>            :   // Read a data point, and use it to initialize the top blob.
<span class="lineNum">      33 </span><span class="lineCov">          4 :   Datum datum;</span>
<span class="lineNum">      34 </span><span class="lineCov">          4 :   datum.ParseFromString(cursor_-&gt;value());</span>
<span class="lineNum">      35 </span>            : 
<span class="lineNum">      36 </span>            :   // Use data_transformer to infer the expected blob shape from datum.
<span class="lineNum">      37 </span><span class="lineCov">          2 :   vector&lt;int&gt; top_shape = this-&gt;data_transformer_-&gt;InferBlobShape(datum);</span>
<span class="lineNum">      38 </span><span class="lineCov">          2 :   this-&gt;transformed_data_.Reshape(top_shape);</span>
<span class="lineNum">      39 </span>            :   // Reshape top[0] and prefetch_data according to the batch_size.
<span class="lineNum">      40 </span><span class="lineCov">          2 :   top_shape[0] = batch_size;</span>
<span class="lineNum">      41 </span><span class="lineCov">          2 :   top[0]-&gt;Reshape(top_shape);</span>
<span class="lineNum">      42 </span><span class="lineCov">         28 :   for (int i = 0; i &lt; this-&gt;prefetch_.size(); ++i) {</span>
<span class="lineNum">      43 </span><span class="lineCov">          8 :     this-&gt;prefetch_[i]-&gt;data_.Reshape(top_shape);</span>
<span class="lineNum">      44 </span>            :   }
<span class="lineNum">      45 </span><span class="lineCov">          6 :   LOG_IF(INFO, Caffe::root_solver())</span>
<span class="lineNum">      46 </span><span class="lineCov">          4 :       &lt;&lt; &quot;output data size: &quot; &lt;&lt; top[0]-&gt;num() &lt;&lt; &quot;,&quot;</span>
<span class="lineNum">      47 </span><span class="lineCov">          8 :       &lt;&lt; top[0]-&gt;channels() &lt;&lt; &quot;,&quot; &lt;&lt; top[0]-&gt;height() &lt;&lt; &quot;,&quot;</span>
<span class="lineNum">      48 </span><span class="lineCov">          4 :       &lt;&lt; top[0]-&gt;width();</span>
<span class="lineNum">      49 </span>            :   // label
<span class="lineNum">      50 </span><span class="lineCov">          2 :   if (this-&gt;output_labels_) {</span>
<span class="lineNum">      51 </span><span class="lineCov">          2 :     vector&lt;int&gt; label_shape(1, batch_size);</span>
<span class="lineNum">      52 </span><span class="lineCov">          2 :     top[1]-&gt;Reshape(label_shape);</span>
<span class="lineNum">      53 </span><span class="lineCov">         28 :     for (int i = 0; i &lt; this-&gt;prefetch_.size(); ++i) {</span>
<span class="lineNum">      54 </span><span class="lineCov">          8 :       this-&gt;prefetch_[i]-&gt;label_.Reshape(label_shape);</span>
<span class="lineNum">      55 </span>            :     }
<span class="lineNum">      56 </span>            :   }
<span class="lineNum">      57 </span><span class="lineCov">          2 : }</span>
<a name="58"><span class="lineNum">      58 </span>            : </a>
<span class="lineNum">      59 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      60 </span><span class="lineCov">     850556 : bool DataLayer&lt;Dtype&gt;::Skip() {</span>
<span class="lineNum">      61 </span>            :   int size = Caffe::solver_count();
<span class="lineNum">      62 </span>            :   int rank = Caffe::solver_rank();
<span class="lineNum">      63 </span><span class="lineCov">     850556 :   bool keep = (offset_ % size) == rank ||</span>
<span class="lineNum">      64 </span>            :               // In test mode, only rank 0 runs, so avoid skipping
<span class="lineNum">      65 </span><span class="lineCov">     850556 :               this-&gt;layer_param_.phase() == TEST;</span>
<span class="lineNum">      66 </span><span class="lineCov">     850556 :   return !keep;</span>
<span class="lineNum">      67 </span>            : }
<a name="68"><span class="lineNum">      68 </span>            : </a>
<span class="lineNum">      69 </span>            : template&lt;typename Dtype&gt;
<span class="lineNum">      70 </span><span class="lineCov">     850556 : void DataLayer&lt;Dtype&gt;::Next() {</span>
<span class="lineNum">      71 </span><span class="lineCov">     850556 :   cursor_-&gt;Next();</span>
<span class="lineNum">      72 </span><span class="lineCov">     850556 :   if (!cursor_-&gt;valid()) {</span>
<span class="lineNum">      73 </span><span class="lineCov">        850 :     LOG_IF(INFO, Caffe::root_solver())</span>
<span class="lineNum">      74 </span>            :         &lt;&lt; &quot;Restarting data prefetching from start.&quot;;
<span class="lineNum">      75 </span><span class="lineCov">        425 :     cursor_-&gt;SeekToFirst();</span>
<span class="lineNum">      76 </span>            :   }
<span class="lineNum">      77 </span><span class="lineCov">     850556 :   offset_++;</span>
<span class="lineNum">      78 </span><span class="lineCov">     850556 : }</span>
<span class="lineNum">      79 </span>            : 
<a name="80"><span class="lineNum">      80 </span>            : // This function is called on prefetch thread</a>
<span class="lineNum">      81 </span>            : template&lt;typename Dtype&gt;
<span class="lineNum">      82 </span><span class="lineCov">      12107 : void DataLayer&lt;Dtype&gt;::load_batch(Batch&lt;Dtype&gt;* batch) {</span>
<span class="lineNum">      83 </span><span class="lineCov">      12107 :   CPUTimer batch_timer;</span>
<span class="lineNum">      84 </span><span class="lineCov">      12107 :   batch_timer.Start();</span>
<span class="lineNum">      85 </span>            :   double read_time = 0;
<span class="lineNum">      86 </span>            :   double trans_time = 0;
<span class="lineNum">      87 </span><span class="lineCov">      12107 :   CPUTimer timer;</span>
<span class="lineNum">      88 </span><span class="lineCov">      24214 :   CHECK(batch-&gt;data_.count());</span>
<span class="lineNum">      89 </span><span class="lineCov">      24214 :   CHECK(this-&gt;transformed_data_.count());</span>
<span class="lineNum">      90 </span><span class="lineCov">      12107 :   const int batch_size = this-&gt;layer_param_.data_param().batch_size();</span>
<span class="lineNum">      91 </span>            : 
<span class="lineNum">      92 </span><span class="lineCov">      24214 :   Datum datum;</span>
<span class="lineNum">      93 </span><span class="lineCov">    1713219 :   for (int item_id = 0; item_id &lt; batch_size; ++item_id) {</span>
<span class="lineNum">      94 </span><span class="lineCov">     850556 :     timer.Start();</span>
<span class="lineNum">      95 </span><span class="lineCov">     850556 :     while (Skip()) {</span>
<span class="lineNum">      96 </span><span class="lineNoCov">          0 :       Next();</span>
<span class="lineNum">      97 </span>            :     }
<span class="lineNum">      98 </span><span class="lineCov">    1701112 :     datum.ParseFromString(cursor_-&gt;value());</span>
<span class="lineNum">      99 </span><span class="lineCov">     850556 :     read_time += timer.MicroSeconds();</span>
<span class="lineNum">     100 </span>            : 
<span class="lineNum">     101 </span><span class="lineCov">     850556 :     if (item_id == 0) {</span>
<span class="lineNum">     102 </span>            :       // Reshape according to the first datum of each batch
<span class="lineNum">     103 </span>            :       // on single input batches allows for inputs of varying dimension.
<span class="lineNum">     104 </span>            :       // Use data_transformer to infer the expected blob shape from datum.
<span class="lineNum">     105 </span><span class="lineCov">      12107 :       vector&lt;int&gt; top_shape = this-&gt;data_transformer_-&gt;InferBlobShape(datum);</span>
<span class="lineNum">     106 </span><span class="lineCov">      12107 :       this-&gt;transformed_data_.Reshape(top_shape);</span>
<span class="lineNum">     107 </span>            :       // Reshape batch according to the batch_size.
<span class="lineNum">     108 </span><span class="lineCov">      12107 :       top_shape[0] = batch_size;</span>
<span class="lineNum">     109 </span><span class="lineCov">      12107 :       batch-&gt;data_.Reshape(top_shape);</span>
<span class="lineNum">     110 </span>            :     }
<span class="lineNum">     111 </span>            : 
<span class="lineNum">     112 </span>            :     // Apply data transformations (mirror, scale, crop...)
<span class="lineNum">     113 </span><span class="lineCov">     850556 :     timer.Start();</span>
<span class="lineNum">     114 </span><span class="lineCov">     850556 :     int offset = batch-&gt;data_.offset(item_id);</span>
<span class="lineNum">     115 </span><span class="lineCov">     850556 :     Dtype* top_data = batch-&gt;data_.mutable_cpu_data();</span>
<span class="lineNum">     116 </span><span class="lineCov">     850556 :     this-&gt;transformed_data_.set_cpu_data(top_data + offset);</span>
<span class="lineNum">     117 </span><span class="lineCov">     850556 :     this-&gt;data_transformer_-&gt;Transform(datum, &amp;(this-&gt;transformed_data_));</span>
<span class="lineNum">     118 </span>            :     // Copy label.
<span class="lineNum">     119 </span><span class="lineCov">     850556 :     if (this-&gt;output_labels_) {</span>
<span class="lineNum">     120 </span><span class="lineCov">     850556 :       Dtype* top_label = batch-&gt;label_.mutable_cpu_data();</span>
<span class="lineNum">     121 </span><span class="lineCov">     850556 :       top_label[item_id] = datum.label();</span>
<span class="lineNum">     122 </span>            :     }
<span class="lineNum">     123 </span><span class="lineCov">     850556 :     trans_time += timer.MicroSeconds();</span>
<span class="lineNum">     124 </span><span class="lineCov">     850556 :     Next();</span>
<span class="lineNum">     125 </span>            :   }
<span class="lineNum">     126 </span><span class="lineCov">      12107 :   timer.Stop();</span>
<span class="lineNum">     127 </span><span class="lineCov">      12107 :   batch_timer.Stop();</span>
<span class="lineNum">     128 </span>            :   DLOG(INFO) &lt;&lt; &quot;Prefetch batch: &quot; &lt;&lt; batch_timer.MilliSeconds() &lt;&lt; &quot; ms.&quot;;
<span class="lineNum">     129 </span>            :   DLOG(INFO) &lt;&lt; &quot;     Read time: &quot; &lt;&lt; read_time / 1000 &lt;&lt; &quot; ms.&quot;;
<span class="lineNum">     130 </span>            :   DLOG(INFO) &lt;&lt; &quot;Transform time: &quot; &lt;&lt; trans_time / 1000 &lt;&lt; &quot; ms.&quot;;
<span class="lineNum">     131 </span><span class="lineCov">      12107 : }</span>
<a name="132"><span class="lineNum">     132 </span>            : </a>
<span class="lineNum">     133 </span>            : INSTANTIATE_CLASS(DataLayer);
<a name="134"><span class="lineNum">     134 </span><span class="lineCov">          5 : REGISTER_LAYER_CLASS(Data);</span></a>
<span class="lineNum">     135 </span>            : 
<span class="lineNum">     136 </span><span class="lineCov">          3 : }  // namespace caffe</span>
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.12</a></td></tr>
  </table>
  <br>

</body>
</html>
